﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Persons.Classes;
using System.Drawing;
using System.IO;

namespace PersonsWinForm
{
    public class DrawPersons:Panel
    {
        Point location;
        int PanelWidth;
        int PanelHeight;
        Persona persona;

        public DrawPersons(Persona per)
        {
            PanelWidth = FViewListOfPersons.PanelWidth;
            PanelHeight = FViewListOfPersons.PanelHeight;
            location = FViewListOfPersons.location;
            persona = per;
        }

        private void FindNewLocation()
        {
            if (location.X + 2*PanelWidth >= Parent.Width)
            {
                location.X = 0;
                location.Y += PanelHeight + 1;
            }
            else
            {
                location.X += PanelWidth + 1;
            }
        }

        public void Draw()
        {
            
            this.Location = location;            
            this.Size = new Size(PanelWidth, PanelHeight);
            if (persona == null)
            {
                this.BackgroundImage = global::PersonsWinForm.Properties.Resources.add_profile;
                this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
                this.Cursor = System.Windows.Forms.Cursors.Hand;
                this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                this.Visible = true;
                this.Click += new System.EventHandler(OpenCreateForm);
            }
            else
            {
                this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                this.Visible = true;
                // 
                // PB_Photo
                // 
                System.Windows.Forms.PictureBox PB_Photo = new System.Windows.Forms.PictureBox();
                this.Controls.Add(PB_Photo);
                PB_Photo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                if (persona.Photo == null)
                {
                    PB_Photo.Image = global::PersonsWinForm.Properties.Resources.photo;
                }
                else
                {
                    MemoryStream memoryStream = new MemoryStream();
                    memoryStream.Write(persona.Photo, 0, persona.Photo.Length);
                    PB_Photo.Image = Image.FromStream(memoryStream);
                }
                PB_Photo.InitialImage = null;
                PB_Photo.Location = new System.Drawing.Point(1, 1);
                PB_Photo.Name = "PB_Photo";
                PB_Photo.Size = new System.Drawing.Size(100, 100);
                PB_Photo.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                // 
                // Lbl_ID
                // 
                System.Windows.Forms.Label Lbl_ID = new System.Windows.Forms.Label();
                this.Controls.Add(Lbl_ID);
                Lbl_ID.AutoSize = true;
                Lbl_ID.Location = new System.Drawing.Point(105, 1);
                Lbl_ID.Name = "Lbl_ID";
                Lbl_ID.Text = "ID: " + persona.ID.ToString();
                // 
                // Lbl_FirstName
                // 
                System.Windows.Forms.Label Lbl_FirstName = new System.Windows.Forms.Label();
                this.Controls.Add(Lbl_FirstName);
                Lbl_FirstName.AutoSize = true;
                Lbl_FirstName.Location = new System.Drawing.Point(105, 19);
                Lbl_FirstName.Name = "Lbl_FirstName";
                Lbl_FirstName.Text = "Имя: " + persona.Fname;
                // 
                // Lbl_LastName
                // 
                System.Windows.Forms.Label Lbl_LastName = new System.Windows.Forms.Label();
                this.Controls.Add(Lbl_LastName);
                Lbl_LastName.AutoSize = true;
                Lbl_LastName.Location = new System.Drawing.Point(105, 37);
                Lbl_LastName.Name = "Lbl_LastName";
                Lbl_LastName.Text = "Фамилия: " + persona.Lname;
                // 
                // Lbl_Age
                // 
                System.Windows.Forms.Label Lbl_Age = new System.Windows.Forms.Label();
                this.Controls.Add(Lbl_Age);
                Lbl_Age.AutoSize = true;
                Lbl_Age.Location = new System.Drawing.Point(105, 55);
                Lbl_Age.Name = "Lbl_Age";
                Lbl_Age.Text = "Возраст: " + persona.Age;
                // 
                // Btn_Edit
                // 
                Button Btn_Edit = new Button();
                this.Controls.Add(Btn_Edit);
                Btn_Edit.Cursor = System.Windows.Forms.Cursors.Hand;
                Btn_Edit.Image = global::PersonsWinForm.Properties.Resources.edit;
                Btn_Edit.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
                Btn_Edit.Location = new System.Drawing.Point(10, 110);
                Btn_Edit.Name = "Btn_Edit";
                Btn_Edit.Size = new System.Drawing.Size(115, 40);
                Btn_Edit.Text = "Редактировать";
                Btn_Edit.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
                Btn_Edit.UseVisualStyleBackColor = true;
                Btn_Edit.Click += new System.EventHandler(OpenEditForm);
                // 
                // Btn_Delete
                // 
                Button Btn_Delete = new Button();
                this.Controls.Add(Btn_Delete);
                Btn_Delete.Cursor = System.Windows.Forms.Cursors.Hand;
                Btn_Delete.Image = global::PersonsWinForm.Properties.Resources.delete;
                Btn_Delete.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
                Btn_Delete.Location = new System.Drawing.Point(140, 110);
                Btn_Delete.Name = "Btn_Delete";
                Btn_Delete.Size = new System.Drawing.Size(90, 40);
                Btn_Delete.Text = "Удалить";
                Btn_Delete.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
                Btn_Delete.UseVisualStyleBackColor = true;
                Btn_Delete.Click += new System.EventHandler(OpenDeleteForm);
            }
            FindNewLocation();
            FViewListOfPersons.location = location;
        }

        public void UpdatePersona(Persona persona)
        {
            if (persona.Photo == null)
            {
                ((PictureBox)this.Controls[0]).Image = global::PersonsWinForm.Properties.Resources.photo;
            }
            else
            {
                MemoryStream memoryStream = new MemoryStream();
                memoryStream.Write(persona.Photo, 0, persona.Photo.Length);
                ((PictureBox)this.Controls[0]).Image = Image.FromStream(memoryStream);
            }
            this.Controls[1].Text = "ID: " + persona.ID.ToString();
            this.Controls[2].Text = "Имя: " + persona.Fname;
            this.Controls[3].Text = "Фамилия: " + persona.Lname;
            this.Controls[4].Text = "Возраст: " + persona.Age.ToString(); 
        }

        public void OpenCreateForm(object sender, EventArgs e)
        {
            FAddPerson fap = new FAddPerson(null, FViewListOfPersons.list);
            fap.ShowDialog(this);
            if (fap.DialogResult == DialogResult.OK)
            {
                DrawPersons dp = new DrawPersons(FViewListOfPersons.list[FViewListOfPersons.list.Count - 1]);
                Parent.Controls.Add(dp);
                dp.Draw();
            }
        }

        public void OpenEditForm(object sender, EventArgs e)
        {
            FAddPerson fap = new FAddPerson(persona, FViewListOfPersons.list);
            fap.ShowDialog(this);
            if (fap.DialogResult == DialogResult.OK)
            {
                UpdatePersona(fap.persona);
            }
        }

        public void OpenDeleteForm(object sender, EventArgs e)
        {
            try
            {
                string msg = "Вы действительно хотите удалить персону с ID = " + persona.ID.ToString() + "?";
                FSaveChanges fsc = new FSaveChanges(msg);
                fsc.ShowDialog(this);
                if (fsc.DialogResult == DialogResult.OK)
                {
                    FViewListOfPersons.list.Delete(persona.ID);
                    System.Windows.Forms.Control parent = Parent;
                    Parent.Controls.Clear();
                    FViewListOfPersons.location = new Point(0, 0);
                    FViewListOfPersons.PanelWidth = parent.Width / 5 - 5;
                    FViewListOfPersons.PanelHeight = parent.Height / 4 - 4;
                    DrawPersons dp = new DrawPersons(null);
                    parent.Controls.Add(dp);
                    dp.Draw();

                    for (int i = 0; i < FViewListOfPersons.list.Count; i++)
                    {
                        dp = new DrawPersons(FViewListOfPersons.list[i]);
                        parent.Controls.Add(dp);
                        dp.Draw();
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        
    }
}
